package com.sunspring.zdcall.controller.commoncontroller;

import com.sunspring.zdcall.common.SimpleResponse;
import com.sunspring.zdcall.common.constant.enums.StateEnum;
import com.sunspring.zdcall.entity.ZdBaseCheckroom;
import com.sunspring.zdcall.service.CheckRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * 检查室控制层
 * @Author WuLei
 * @Date 2018/11/28  14:57
 */
@RestController
public class CheckroomController {

    /**
     * 后台地址前缀（经过拦截器）
     */
    private final String backend_prefix = "/sys/checkroom";

    /**
     * 前端地址前缀（不经过拦截器）
     */
    private final String front_prefix = "/checkroom";

    @Autowired
    private CheckRoomService crs;


    /**
     * 根据科室ID查看所有的检查室
     * @param deptId 科室ID
     */
    @GetMapping(backend_prefix + "/byDept")
    public SimpleResponse byDept(String deptId){
        ZdBaseCheckroom checkroom = new ZdBaseCheckroom();
        checkroom.setDepartmentId(deptId);
        return SimpleResponse.success(crs.getSelective(checkroom));
    }

    /**
     * 根据科室ID查看所有已启用的检查室
     * @param deptId 科室ID
     */
    @GetMapping(front_prefix + "/actByDept")
    public SimpleResponse actByDept(String deptId){
        ZdBaseCheckroom checkroom = new ZdBaseCheckroom();
        checkroom.setDepartmentId(deptId);
        checkroom.setCheckroomState(StateEnum.enable.getCode());
        return SimpleResponse.success(crs.getSelective(checkroom));
    }


    /**
     * 根据检查室ID删除检查室
     * @param roomId 检查室Id
     */
    @PostMapping(backend_prefix +"/delete")
    public SimpleResponse delete(String roomId){
        crs.delete(roomId);
        return SimpleResponse.success();
    }

    /**
     * 根据检查室ID修改检查室信息
     * @param checkRoomInfo ZdBaseCheckroom
     */
    @PostMapping(backend_prefix + "/update")
    public SimpleResponse update(ZdBaseCheckroom checkRoomInfo){
        ZdBaseCheckroom dbCheckRoomInfo = crs.getById(checkRoomInfo.getCheckRoomId());
        //是否修改前缀
        if (!Objects.equals(dbCheckRoomInfo.getQueuePrefix(),checkRoomInfo.getQueuePrefix())) {
            //前缀唯一性校验
            crs.checkPrefix(checkRoomInfo.getDepartmentId(),checkRoomInfo.getQueuePrefix());
        }
        crs.updateById(checkRoomInfo);
        return SimpleResponse.success();
    }

    /**
     * 停用/启用检查室
     */
    @PostMapping(backend_prefix + "/switch")
    public SimpleResponse switchCheckroom(String deptId,String checkroomId) {
        crs.switchCheckroom(deptId,checkroomId);
        return SimpleResponse.success();
    }

    /**
     * 所有正在启用中的科室和检查室
     */
    @GetMapping(front_prefix + "/allAct")
    public SimpleResponse allAct() {
        return SimpleResponse.success(crs.getAllAct());
    }

}
